
// 加载所有采购订单
function loadPurchaseOrders() {
    fetch(API_BASE_URL)
        .then(response => {
            if (!response.ok) {
                throw new Error('网络响应错误');
            }
            return response.json();
        })
        .then(orders => {
            renderPurchaseOrdersTable(orders);
        })
        .catch(error => {
            console.error('加载采购订单失败:', error);
            alert('加载采购订单失败: ' + error.message);
            // 使用mock数据
            renderPurchaseOrdersTable(mockPurchaseOrders);
        });
}

// 渲染采购订单表格
function renderPurchaseOrdersTable(orders) {
    const ordersTableBody = document.querySelector('#purchase-orders-table tbody');
    ordersTableBody.innerHTML = '';

    if (!orders || orders.length === 0) {
        ordersTableBody.innerHTML = '<tr><td colspan="8" style="text-align: center;">暂无采购订单数据</td></tr>';
        return;
    }

    orders.forEach(order => {
        const row = document.createElement('tr');
        row.innerHTML = `
            <td>${order.orderNumber}</td>
            <td>${order.supplierID}</td>
            <td>¥${order.totalAmount.toFixed(2)}</td>
            <td>${formatDateTime(order.orderDate)}</td>
            <td>${getStatusText(order.status)}</td>
            <td>${formatDateTime(order.creationTime)}</td>
            <td>
                <button class="btn btn-sm btn-primary view-details-btn" data-id="${order.id}">查看</button>
            </td>
        `;
        ordersTableBody.appendChild(row);
    });

    // 添加查看详情的事件监听
    document.querySelectorAll('.view-details-btn').forEach(btn => {
        btn.addEventListener('click', function() {
            const orderId = this.getAttribute('data-id');
            getPurchaseOrderById(orderId);
        });
    });
}

// 页面加载完成后执行
document.addEventListener('DOMContentLoaded', function() {
    // 加载采购订单列表
    loadPurchaseOrders();

    // 其他事件监听...
});

// 根据ID获取采购订单详情
function getPurchaseOrderById(orderId) {
    fetch(`${API_BASE_URL}/${orderId}`)
        .then(response => {
            if (!response.ok) {
                throw new Error('获取采购订单失败');
            }
            return response.json();
        })
        .then(order => {
            displayPurchaseOrderDetails(order);
        })
        .catch(error => {
            console.error('获取采购订单失败:', error);
            alert('获取采购订单失败: ' + error.message);
        });
}

// 显示采购订单详情
function displayPurchaseOrderDetails(order) {
    document.getElementById('detail-order-number').innerText = order.orderNumber;
    document.getElementById('detail-supplier-id').innerText = order.supplierID;
    document.getElementById('detail-total-amount').innerText = `¥${order.totalAmount.toFixed(2)}`;
    document.getElementById('detail-order-date').innerText = formatDateTime(order.orderDate);
    document.getElementById('detail-status').innerText = getStatusText(order.status);
    document.getElementById('detail-creation-time').innerText = formatDateTime(order.creationTime);
    showSection(document.getElementById('order-details'));
}
